package com.example.arithmeticleetcode.leetcode.dec;

import java.util.Arrays;

/**
 * @program: arithmetic-leetcode
 * @description: 分发糖果
 * @author: FangZhen
 * @create: 2020-12-24 09:22
 **/
public class Demo23 {

    public static void main(String[] args) {
        int[] ratings = {1, 0, 2};
        candy(ratings);
    }

    public static int candy(int[] ratings) {
        int[] left = new int[ratings.length];
        int[] right = new int[ratings.length];
//        Arrays.fill(left, 1);
//        Arrays.fill(right, 1);
        left[0] = 1;
        for (int i = 1; i < ratings.length; i++) {
            left[i] = ratings[i] > ratings[i - 1] ? left[i - 1] + 1 : 1;
        }
        right[ratings.length - 1] = 1;
        int count = left[ratings.length - 1];
        for (int i = ratings.length - 2; i >= 0; i--) {
            right[i] = ratings[i] > ratings[i + 1] ? right[i + 1] + 1 : 1;
            count += Math.max(left[i], right[i]);
        }
        return count;
    }
}
